var log = require('../../log.js')

Page({
  data: {
    loading: true,
    nickName: '',
    avatarUrl: '',
    isCanDraw: false,
    isAdmin: false,
    activeNames: ['1']
  },
  onLoad(options) {
    console.log('列表携带id', options)
    var id = options.id
    wx.cloud.database().collection('song')
      .doc(id)
      .get()
      .then(res => {
        log.info(res)
        this.setData({
          song: res.data,
          time: res.data.time.toLocaleString(),
          loading: false
        })
        console.log(res.data)
        wx.setStorageSync('songTitle', res.data.title)
        wx.setStorageSync('songSinger', res.data.singer)
        if (res.data.isAnonymous == false) {
          wx.setStorageSync('nickName', res.data.pickerInfo.nickName)
          wx.setStorageSync('avatarUrl', res.data.pickerInfo.avatarUrl)
        } else {
          wx.setStorageSync('nickName', '匿名')
          wx.setStorageSync('avatarUrl', '')
        }
        if (res.data.state == 0) {
          var state = '未审核'
        } else if (res.data.state == 1) {
          var state = '已通过'
        } else if (res.data.state == 2) {
          var state = '不通过'
        }
        wx.setStorageSync('songState', state)
      })
      .catch(err => {
        this.setData({
          loading: false
        })
        console.log(err)
        log.error(err)
      })
    this.isAdmin()
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  getLyric() {
    wx.request({
      url: 'https://musicapi.miniprogram.nr0.top/music/list',
      data: {
        p: '1',
        n: '1',
        w: this.data.song.title + ' ' + this.data.song.singer
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: (res) => {
        if (res.data.data.list.length != 0) {
          console.log(res.data.data.list[0])
          let songMid = res.data.data.list[0].songmid
          wx.request({
            url: 'https://musicapi.miniprogram.nr0.top/music/lyric',
            data: {
              songmid: songMid
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: (res) => {
              console.log(res)
              if (res.data.data.lyric != undefined) {
                this.setData({
                  lyric: res.data.data.lyric
                })
              } else {
                this.setData({
                  lyric: '未找到歌词'
                })
              }
            }
          })
        } else {
          this.setData({
            lyric: '未找到歌词'
          })
        }
      }
    })
  },
  isAdmin() {
    wx.cloud.callFunction({
        name: 'adminAuth'
      })
      .then(res => {
        console.log('adminAuth', res.result)
        if (res.result) {
          this.setData({
            isAdmin: true,
          })
          this.getLyric()
        }
      })
  },
  handleClose() {
    this.setData({
      isCanDraw: !this.data.isCanDraw
    })
  },
  draw() {
    log.info('Draw songPost')
    this.setData({
      isCanDraw: !this.data.isCanDraw
    })
  },
  onShareAppMessage: function (res) {
    var pages = getCurrentPages()
    var currentPage = pages[pages.length - 1]
    var url = currentPage.route
    var options = currentPage.options
    var urlWithArgs = `/${url}?`
    for (var key in options) {
      var value = options[key]
      urlWithArgs += `${key}=${value}&`
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
    console.log(urlWithArgs)
    return {
      title: 'TLradio - 点歌详情',
      path: urlWithArgs,
      //imageUrl: '../../images/share1.png'
    }
  },
  onShareTimeline: function () {
    return {
      title: 'TLradio - 点歌详情',
      query: this.data.id
    }
  }
})